﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Administration;

namespace AIS.SharePoint.AspNetFBA.Code
{
    public class Log : SPDiagnosticsServiceBase
    {
        /// <summary>
        /// The product name to log into the ULS uder
        /// </summary>
        private const string _PRODUCTNAME = "AIS.SharePoint.AspNetFBA";

        /// <summary>
        /// Singleton representation of this logger
        /// </summary>
        public static Log SingletonLog { get; set; }

        /// <summary>
        /// Logging categories
        /// </summary>
        public enum Category
        {
            None = 0,
            Deployment = 100,
            Provisioning = 200,
            CustomAction = 300,
            Rendering = 400,
            WebPart = 500,
            Processing = 600

        }


        /// <summary>
        /// Constructor
        /// Override the and setup the product name
        /// </summary>
        private Log() : base(_PRODUCTNAME, SPFarm.Local) { }

        protected override IEnumerable<SPDiagnosticsArea> ProvideAreas()
        {
            List<SPDiagnosticsCategory> Categories = new List<SPDiagnosticsCategory>();
            foreach (string CatName in Enum.GetNames(typeof(Category)))
            {
                uint catId = (uint)(int)Enum.Parse(typeof(Category), CatName);
                Categories.Add(new SPDiagnosticsCategory(CatName, TraceSeverity.Verbose, EventSeverity.Error, 0, catId));
            }

            yield return new SPDiagnosticsArea(_PRODUCTNAME, Categories);
        }

        /// <summary>
        /// Log an error message to the ULS
        /// </summary>
        /// <param name="ex">Error to log</param>
        public static void Error(Exception ex)
        {
            Error(ex, TraceSeverity.Unexpected, Category.None);
        }

        /// <summary>
        /// Log an error message to the ULS
        /// </summary>
        /// <param name="severity">Severity of the error</param>
        /// <param name="category">Category of the error</param>
        public static void Error(Exception ex, TraceSeverity severity, Category category)
        {
            Error(ex.Message + " " +  ex.StackTrace, severity, category);
        }

        /// <summary>
        /// Log an error message to the ULS
        /// </summary>
        /// <param name="message">Message to log</param>
        /// <param name="severity">Severity of the error</param>
        /// <param name="category">Category of the error</param>
        public static void Error(string message, TraceSeverity severity, Category category)
        {
            try
            {
                if (SingletonLog == null) { SingletonLog = new Log(); }
                SPDiagnosticsCategory SelectedCat = SingletonLog.Areas[_PRODUCTNAME].Categories[(uint)category];
                SingletonLog.WriteTrace(0, SelectedCat, severity, message); 
            }
            catch (Exception)
            {
                // not a good thing to be here, but not much we can do about it.
            }
        }

    }
}
